//
//  SchoolRecordViewController.m
//  Learn
//
//  Created by zyl_macbookpro on 2024/4/6.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "SchoolRecordViewController.h"
#import "SchoolRecordBarChart.h"
#import "MineAPIManager.h"
@interface SchoolRecordViewController ()

@property(nonatomic ,strong)UIScrollView *containView;

@property(nonatomic ,strong)UIView *topBtnView;

@property(nonatomic ,strong)UIView *lineView;

@property(nonatomic ,strong)UIButton *selectedBtn;

@property(nonatomic ,strong)SchoolRecordBarChart *barChart;

@property(nonatomic ,strong)NSDictionary *data;

@end

@implementation SchoolRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:lClearColor] backgroundColor:lClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(51, 51, 51, 1),NSFontAttributeName:MFontWithSize(18)} enableScrollEdgeAppearance:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学习成绩";
    // Do any additional setup after loading the view.
    [self setBackButtonIsHidden:NO];
    [self.view addSubview:self.topBtnView];
    self.view.backgroundColor = rgba(245, 245, 245, 1);
    [self createRightBarButtonItem];
  
}
- (void)createRightBarButtonItem
{
    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 56, 44)];
    [button setTitle:@"学习记录" forState:UIControlStateNormal];
    button.titleLabel.font = MFontWithSize(14.f);
    [button setupEdgeInsetsByDirection:DFButtonImageDirectionBottom];
    [button setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goMyRecord) forControlEvents:UIControlEventTouchUpInside];
   
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItems =@[item];
}
-(void)showMoreButtonClick
{
    
}
- (void)chooseMonth:(UIButton *)btn
{
    if (self.selectedBtn != btn) {
        self.selectedBtn = btn;
        [UIView animateWithDuration:0.3 animations:^{
            self.lineView.centerX = self.selectedBtn.centerX;
        }];
    }
}
- (UIScrollView *)containView
{
    if (!_containView) {
        _containView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.topBtnView.bottom, lScreenWidth, lScreenHeight - self.topBtnView.bottom - lBottomDangerousAreaHeight)];
        _containView.backgroundColor = lClearColor;
        _containView.showsVerticalScrollIndicator = NO;
        _containView.showsHorizontalScrollIndicator = NO;
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(10,10, lScreenWidth - 20, 504)];
        topView.backgroundColor = lWhiteColor;
        [topView setAllCornersWithCornerRadius:15];
        [topView addSubview: separateLineWithColor(CGRectMake(0, 148, lScreenWidth - 20, .5), rgba(229, 229, 229, 1))];
        [topView addSubview:self.barChart];
        self.barChart.y = 254;
        
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lScreenWidth - 20 - 20, 22)];
        titleLab.font = MFontWithSize(16);
        titleLab.text = @"学习总体情况";
        titleLab.textColor = rgba(51, 51, 51, 1);
        titleLab.textAlignment = NSTextAlignmentLeft;
        [topView addSubview:titleLab];
        double width = (lScreenWidth - 40)/3.00;
        for (int i= 0; i < 3; i++) {
            UILabel *titleLab1 = [[UILabel alloc] initWithFrame:CGRectMake(10 + width*i, 15 + titleLab.bottom, width, 22)];
            titleLab1.font = MFontWithSize(16);
            titleLab1.text = i == 0? @"完成作业":i == 1? @"订正错题":@"自主练习";;
            titleLab1.textColor = i == 0? rgba(12, 183, 108, 1):i == 1? rgba(250, 25, 44, 1):rgba(0, 122, 255, 1);
            titleLab1.textAlignment = i == 0? NSTextAlignmentLeft:i == 1? NSTextAlignmentCenter:NSTextAlignmentRight;
            [topView addSubview:titleLab1];
            
            UILabel *titleLab2 = [[UILabel alloc] initWithFrame:CGRectMake(10 + width*i, titleLab1.bottom + 15, width, 22)];
            titleLab2.font = MFontWithSize(16);
            NSString *key =  i == 0? @"done":i == 1? @"fix":@"lx_self";
            titleLab2.text = [NSString stringWithFormat:@"%d份",[self.data[key] intValue]];
            titleLab2.textColor = rgba(51, 51, 51, 1);
            titleLab2.textAlignment = titleLab1.textAlignment;
            [topView addSubview:titleLab2];
            
            
            UILabel *titleLab3 = [[UILabel alloc] initWithFrame:CGRectMake(10 + width*i, titleLab2.bottom + 10, width, 17)];
            titleLab3.font = MFontWithSize(12);
            NSString *key2 =  i == 0? @"class_sort_done":i == 1? @"done_fix":@"class_sort_self";
            titleLab3.text = i == 0?[NSString stringWithFormat:@"班级排名：%d份",[self.data[key2] intValue]]:i == 1?[NSString stringWithFormat:@"已订正%d题",[self.data[key2] intValue]]:[NSString stringWithFormat:@"班级排名：%d",[self.data[key2] intValue]];
            titleLab3.textColor = rgba(142, 142, 142, 1);
            titleLab3.textAlignment = titleLab1.textAlignment;
            [topView addSubview:titleLab3];
            
        }
        
        NSString *finish = [NSString stringWithFormat:@"%.2f%@",[self.data[@"mission_done_lv"] doubleValue],@"%"];
        NSString *finishUp = [NSString stringWithFormat:@"%.2f%@",[self.data[@"mission_done_lv_last"] doubleValue],@"%"];
        NSMutableAttributedString *finishAtt = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"作业完成率：%@（对比上个周期%@）",finish,finishUp]];
        [finishAtt addAttribute:NSForegroundColorAttributeName value:rgba(51, 51, 51, 1) range:[finishAtt.string rangeOfAll]];
        [finishAtt addAttribute:NSForegroundColorAttributeName value:rgba(12, 183, 108, 1) range:[finishAtt.string rangeOfString:finishUp]];
        UILabel *finishLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 161, lScreenWidth - 20 - 20, 24)];
        finishLab.font = MFontWithSize(12);
        finishLab.attributedText = finishAtt;
        finishLab.textAlignment = NSTextAlignmentLeft;
        [topView addSubview:finishLab];
        
        NSString *sorce = [NSString stringWithFormat:@"%.2f%@",[self.data[@"mission_get_score_lv"] doubleValue],@"%"];
        NSString *sorceUp = [NSString stringWithFormat:@"%.2f%@",[self.data[@"mission_get_score_lv_last"] doubleValue],@"%"];
        NSMutableAttributedString *sorceAtt = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"作业得分率：%@（对比上个周期%@）",sorce,sorceUp]];
        [sorceAtt addAttribute:NSForegroundColorAttributeName value:rgba(51, 51, 51, 1) range:[sorceAtt.string rangeOfAll]];
        [sorceAtt addAttribute:NSForegroundColorAttributeName value:rgba(12, 183, 108, 1) range:[sorceAtt.string rangeOfString:sorceUp]];
        UILabel *sorceLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 161 + 24, lScreenWidth - 20 - 20, 24)];
        sorceLab.font = MFontWithSize(12);
        sorceLab.attributedText = sorceAtt;
        sorceLab.textAlignment = NSTextAlignmentLeft;
        [topView addSubview:sorceLab];
        // 图表说明
        double x = 10;
        for (int i= 0; i < 2; i++) {
            UILabel *titleLabE = [[UILabel alloc] initWithFrame:CGRectMake(x, 219, 30, 20)];
            titleLabE.font = MFontWithSize(12);
            titleLabE.text = i == 0? @"我的成绩":@"班级平均成绩";;
            titleLabE.textColor = rgba(51, 51, 51, 1);
            titleLabE.textAlignment = NSTextAlignmentLeft;
            [topView addSubview:titleLabE];
            [titleLabE sizeToFit];
            titleLabE.height = 20;
            UIView *view = [[UIView alloc] init];
            view.frame = CGRectMake(73,369,14,4);
            view.x = titleLabE.right + 5;
            view.backgroundColor = i == 0?rgba(12, 183, 108, 1):rgba(255, 153, 23, 1);
            [view setAllCornersWithCornerRadius:2];
            view.centerY = titleLabE.centerY;
            x = view.right + 30;
            [topView addSubview:titleLabE];
            [topView addSubview:view];
            
        }
        
        UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(10,topView.bottom + 10, lScreenWidth - 20, 169)];
        bottomView.backgroundColor = lWhiteColor;
        [bottomView setAllCornersWithCornerRadius:15];
        [bottomView addSubview: separateLineWithColor(CGRectMake(0, 42, lScreenWidth - 20, .5), rgba(229, 229, 229, 1))];
        UILabel *titleLabB = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, lScreenWidth - 56 - 20, 22)];
        titleLabB.font = MFontWithSize(16);
        titleLabB.text = @"作业分析";
        titleLabB.textColor = rgba(51, 51, 51, 1);
        titleLabB.textAlignment = NSTextAlignmentLeft;
        [bottomView addSubview:titleLabB];
        
//        UIButton *btnMore = ({
//            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//            button.frame = CGRectMake(lScreenWidth - 20 - 51, 0,51 , 42);
//            [button setTitle:@"更多" forState:UIControlStateNormal];
//            button.titleLabel.font = RFontWithSize(14.f);
//            [button setImage:[UIImage imageNamed:@"ic_arrow_gray"] forState:UIControlStateNormal];
//            [button setTitleColor:rgba(142, 142, 142, 1) forState:UIControlStateNormal];
//            [button addTarget:self action:@selector(showMoreButtonClick) forControlEvents:UIControlEventTouchUpInside];
//            [button layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleRight imageTitleSpace:5];
//            button;
//        });
//        [bottomView addSubview:btnMore];
        
        NSArray *array = self.data[@"mission_info"];
        double xB = 10;
        double heightB = 60;
        double titleW = (lScreenWidth - 20 - 20 - 36 - 58)/3.00;
        NSInteger count = array.count > 4? 4:array.count;
        for (int i= 0; i < count + 1; i++) {
            
            NSString *titleA1 = @"明细";
            NSString *titleA2 = @"次数";
            NSString *titleA3 = @"完成率";
            NSString *titleA4 = @"得分率";
            UIColor *color1 = rgba(142, 142, 142, 1);
            UIColor *color2 = rgba(142, 142, 142, 1);
            
            if (i > 0) {
                
                NSDictionary *dic = array[i - 1];
                color2 = rgba(51, 51, 51, 1);
                titleA1 = dic[@"title"];
                titleA2 = [NSString stringWithFormat:@"%.f",[dic[@"num"] doubleValue]];
                titleA3 = [NSString stringWithFormat:@"%.2f%@",[dic[@"done_lv"] doubleValue],@"%"];
                titleA4 = [NSString stringWithFormat:@"%.2f%@",[dic[@"score_lv"] doubleValue],@"%"];
            }
            double w = i == 0 ? 36:i == 4?58:titleW;
            NSTextAlignment textAlignment = i == 0 ? NSTextAlignmentLeft:i == 4?NSTextAlignmentRight:NSTextAlignmentCenter;
            UILabel *titleLabA1 = [[UILabel alloc] initWithFrame:CGRectMake(xB, 60, w, 16)];
            titleLabA1.font = MFontWithSize(12);
            titleLabA1.text = titleA1;
            titleLabA1.textColor = color1;
            titleLabA1.textAlignment = textAlignment;
            [bottomView addSubview:titleLabA1];
           
            UILabel *titleLabA2 = [[UILabel alloc] initWithFrame:CGRectMake(xB, titleLabA1.bottom + 10, w, 16)];
            titleLabA2.font = MFontWithSize(12);
            titleLabA2.text = titleA2;
            titleLabA2.textColor = color2;
            titleLabA2.textAlignment = textAlignment;
            [bottomView addSubview:titleLabA2];
            
            UILabel *titleLabA3 = [[UILabel alloc] initWithFrame:CGRectMake(xB, titleLabA2.bottom + 10, w, 16)];
            titleLabA3.font = MFontWithSize(12);
            titleLabA3.text = titleA3;
            titleLabA3.textColor = color2;
            titleLabA3.textAlignment = textAlignment;
            [bottomView addSubview:titleLabA3];
            
            
            UILabel *titleLabA4 = [[UILabel alloc] initWithFrame:CGRectMake(xB, titleLabA3.bottom + 10, w, 16)];
            titleLabA4.font = MFontWithSize(12);
            titleLabA4.text = titleA4;
            titleLabA4.textColor = color2;
            titleLabA4.textAlignment = textAlignment;
            [bottomView addSubview:titleLabA4];
            xB = xB + w;
            heightB = titleLabA4.bottom + 10;
        }
        bottomView.height = heightB;
       
        [_containView addSubview:topView];
        [_containView addSubview:bottomView];
        _containView.contentSize = CGSizeMake(lScreenWidth, bottomView.bottom + 10);
        
    }
    return _containView;
}
- (void)goMyRecord
{
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"LearningRecordViewController"];
    vc.paraObject = self.selectedBtn.tag - 10000 == 0?@{@"month":@"1"}:self.selectedBtn.tag - 10000 == 1?@{@"month":@"2"}:@{@"month":@"6"};
    [self.navigationController pushViewController:vc animated:YES];

}

- (UIView *)topBtnView
{
    if (!_topBtnView) {
        _topBtnView = [[UIView alloc] initWithFrame:CGRectMake(0,lTopBarHeight + 2, lScreenWidth, 43.5)];
        [_topBtnView addSubview: separateLineWithColor(CGRectMake(0, 43, lScreenWidth, .5), rgba(229, 229, 229, 1))];
        NSArray *title = @[@"一个月",@"二个月",@"六个月"];
        double widthTitle = [MethodTool getTextWidthWithStr:title.firstObject font:MFontWithSize(18.f) ViewHeight:41];
        double space = (lScreenWidth - 40 - widthTitle*3)/2.0;
        for (int i = 0; i < title.count; i++) {
            UIButton *monthLBtn = ({
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(20 + (space + widthTitle)*i ,2, widthTitle, 41);
                [button setTitle:title[i] forState:UIControlStateNormal];
                button.titleLabel.font = MFontWithSize(18.f);
                [button setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateSelected];
                [button setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
                [button addTarget:self action:@selector(chooseMonth:) forControlEvents:UIControlEventTouchUpInside];
                button;
            });
            monthLBtn.tag = i + 10000;
            if (i == 0) {
                self.selectedBtn = monthLBtn;
            }
            
            [_topBtnView addSubview:monthLBtn];
        }
        
        [_topBtnView addSubview: self.lineView];
        self.lineView.centerX = self.selectedBtn.centerX;
    }
    return _topBtnView;
}
- (UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.frame = CGRectMake(95,40,28,3);
        _lineView.backgroundColor = rgba(12, 183, 108, 1);
        [_lineView setAllCornersWithCornerRadius:1.5];
    }
    return _lineView;
}
- (SchoolRecordBarChart *)barChart
{
    if (!_barChart) {
        _barChart = [[SchoolRecordBarChart alloc] initWithFrame:CGRectMake(8, 0, lScreenWidth - 20 - 18, 235)];
       
    }

    
    NSArray *xArray = self.data[@"date_list"]?self.data[@"date_list"]:@[];
    NSMutableArray *yArray = @[].mutableCopy;
    NSMutableArray *aArray = @[].mutableCopy;
    if (self.data[@"self_score_list"]) {
        [yArray addObject:self.data[@"self_score_list"]];
        [aArray addObjectsFromArray:self.data[@"self_score_list"]];
    }
    
    if (self.data[@"class_score_list"]) {
        [yArray addObject:self.data[@"class_score_list"]];
        [aArray addObjectsFromArray:self.data[@"class_score_list"]];
    }
    double max = [aArray.firstObject doubleValue];
    
    for (NSString *score in aArray) {
        if ([score doubleValue] > max) {
            max = [score doubleValue];
        }
    }
    
   
    [_barChart setChooseRange:CGRangeMake(max, 0)];
    [_barChart setColors:@[rgba(12, 183, 108, 1),rgba(255, 153, 23, 1) ]];
    [_barChart setYValues:yArray];
    [_barChart setXLabels:xArray];
    
    [_barChart strokeChart];
    return _barChart;
}
- (void)setSelectedBtn:(UIButton *)selectedBtn
{
    _selectedBtn = selectedBtn;
    NSDictionary *dic =  self.selectedBtn.tag - 10000 == 0?@{@"month":@"1"}:self.selectedBtn.tag - 10000 == 1?@{@"month":@"2"}:@{@"month":@"6"};
    [MineAPIManager GetMissionHistoryWithParam:dic result:^(NSDictionary * _Nonnull dic) {
        if ([dic[@"code"] intValue] == 1) {
            self.data = dic[@"data"];
            if (self.containView.superview) {
                [self.containView removeFromSuperview];
                self.containView = nil;
                self.barChart = nil;
            }
            [self.view addSubview:self.containView];
        }
    }];
}
@end
